package com.passive.lock.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passive.lock.controller.BaseController;
import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.user.UserService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("us")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;

	/**
	 * 用户信息管理
	 *
	 * @param model
	 * @returns
	 */

	@RequestMapping("/userlist")
	@ResponseBody
	public Result userList(@RequestParam String account, @RequestParam String name, @RequestParam String deptid,
			@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer status,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUser = (TUserVo) req.getSession().getAttribute("tUserVo");
		Integer offset = PageUtil.getOffset(page, limit);
		TUserVo tUserVo = new TUserVo();
		tUserVo.setAccount(account);
		tUserVo.setName(name);
		tUserVo.setGroupid(deptid);
		try {
			List<TUserVo> list = userService.userList(tUser.getGroupid(),tUserVo, offset, limit);
			int count = userService.userListCount(tUser.getGroupid(),tUserVo);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/adduser")
	@ResponseBody
	public Result addUser(@RequestBody TUserVo uservo) {
		try {
			Result result = userService.addUser(uservo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updateuser")
	@ResponseBody
	public Result updateUser(@RequestBody TUserVo uservo) {
		try {
			Result result = userService.updateUser(uservo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteuser")
	@ResponseBody
	public Result deleteUser(@RequestBody TUserVo uservo,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo user = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = userService.deleteUser(uservo,user);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteuserlist")
	@ResponseBody
	public Result deleteUserList(@RequestBody List<Integer> userids) {
		try {
			Result result = userService.deleteUserList(userids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	@RequestMapping(value = "/queryuserlist")
	@ResponseBody
	public Result queryUserList(@RequestParam(required = false) String deptid,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo user = (TUserVo) req.getSession().getAttribute("tUserVo");
		if(deptid == null||"".equals(deptid)){
			deptid = user.getGroupid();
		}
		try {
			Result result = userService.queryUserList(deptid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	
	@RequestMapping(value = "/userlistbygroupid")
	@ResponseBody
	public Result userListByGroupid(@RequestParam(required = false) String deptid,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo user = (TUserVo) req.getSession().getAttribute("tUserVo");
		if(deptid == null||"".equals(deptid)){
			deptid = user.getGroupid();
		}
		try {
			Result result = userService.userListByGroupid(deptid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	@RequestMapping(value = "/updatepassword")
	@ResponseBody
	public Result updatePassword(@RequestBody TUserVo uservo) {
		try {
			Result result = userService.updatePassword(uservo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ok();
		}
	}
	
	@RequestMapping(value = "/checkpassword")
	@ResponseBody
	public Result checkPassword(@RequestBody TUserVo uservo) {
		try {
			Result result = userService.checkPassword(uservo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ok();
		}
	}

}
